<?php
            
use app\model\Util; 
use app\model\Theme; 
use app\model\Worker; 


class Record_Page extends PluginWebPage {
    //获取记录
    function gain($method)
    {
        $method = 'gift_gain';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }

        $theme = Theme::getThemeById($themeid);
        $themelist = Theme::getAllTheme();

        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;

        $shangjiaid = isset($_P['shangjiaid'])?$_P['shangjiaid']:0;
        $status = isset($_P['status'])?$_P['status']:0;
        $type = isset($_P['type'])?$_P['type']:0;
        $time_type = isset($_P['time_type'])?$_P['time_type']:1;
        $classify =$_P['classify']?$_P['classify']:'';
        $uid = isset($_P['uid'])?intval($_P['uid']):0;
        $gift_id = $_P['gift_id']?intval($_P['gift_id']):0;
        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        $createtime = $_P['createtime'];
        $starttime = empty($createtime['starttime'])?date('Y-m-d H:i:s',$theme['begintime']):$createtime['starttime'];
        $endtime = empty($createtime['endtime'])?date('Y-m-d ',time()).' 23:59:59':$createtime['endtime'];

        $where_arr['type'] = $type;
        $where_arr['status'] = $status;
        $where_arr['uid'] = $uid;
        $where_arr['shangjiaid'] = $shangjiaid;
        $where_arr['classify'] = $classify;
        $where_arr['gift_id'] = $gift_id;
        $where_arr['time_type'] = $time_type;
        $where_arr['starttime'] = $starttime;
        $where_arr['endtime'] = $endtime;
        $where_arr['search_data'] = $search_data;

        $gift_list = giftModel::getGiftAllList($themeid,1,0);
        extract(giftModel::getGiftCount($themeid,$where_arr));
        $count = giftModel::getGainRecord($themeid,$where_arr,0,0,true);
        $total = giftModel::getGiftStatusList($themeid);
        $status_list = [
                   ['status' => 0,'title' => '全部', 'total' => $total['count']],
                   ['status' => 1,'title' => '未核销', 'total' => $total['gain_num']],
                   ['status' => 2,'title' => '已核销', 'total' => $total['exchange_num']]
        ];

        $gift_config = GiftModel::getGiftConfig($themeid);
        $tuan_config_data = array_column($gift_config['tuan_gift'], 'number', 'taskid');
        $invite_config_data = array_column( $gift_config['invite_gift'], 'number', 'taskid');

        if(checksubmit('export_submit', true)){
            $count = $total['count'];
            $header = array(
                'id' => 'ID',
                'nickname' => '昵称',
                'username' => '姓名',
                'mobile' => '电话',
                'gift_name' => '礼品',
                'type' => '类型',
                'classify' => '来源',
                'status' => '状态',
                'worker' => '员工',
                'created_at' => '时间'
            );
            $keys = array_keys($header);
            $html = "\xEF\xBB\xBF";
            foreach ($header as $li) {
                $html .= $li . "\t ,";
            }
            $html .= "\n";

            $ex_page_index = max(1,intval($_P['ex_page']));
            $ex_page_size = 5000;
            $ex_page_start = ($ex_page_index-1)*$page_size;
            $page_total = ceil($count/$ex_page_size);

            $list = giftModel::getGainRecord($themeid,$where_arr,$ex_page_start,$ex_page_size);
            foreach ($list as $row) {
                $row['nickname'] = str_replace(',','',$row['nickname']);
                $row['nickname'] = str_replace('"','',$row['nickname']);
                $gift_id = $row['gift_id'];
                if($gift_list[$gift_id]){
                    $gift = $gift_list[$gift_id];
                    $gift_name = $gift['name'];
                    $gift_img = $gift['top_img'];
                    $gift_price = $gift['price'];
                    $gift_type =  $gift['type'];
                }else{
                    $gift_name = '';
                    $gift_img = '';
                    $gift_price = '';
                    $gift_type = 1;
                }
                $row['gift_name'] = $gift_name;
                $row['price'] = $gift_price;
                $row['status']=  giftModel::statusList($row['status'],1);
                $row['type'] = $gift_type==1?'实物':'红包';

                $taskid = $row['taskid'];
                if($row['classify']=='tuan_gift'){
                   $row['classify'] = '成团'.$tuan_config_data[$taskid].'人';
                }else{
                   $row['classify'] = '邀请'.$invite_config_data[$taskid].'人';
                }

                foreach ($keys as $key) {
                    $data[] = $row[$key];
                }
                if (isset($data)){
                    $user[] = implode("\t ,", $data) . "\t ,";
                    unset($data);
                }
            }
            $html .= implode("\n", $user)."\n";
            //新版本导出合并
            $is_end = 0;
            if($ex_page_index>=$page_total){
                $is_end = 1;
            }
            Util::WxJump($_S,$html,'邀请礼礼品记录',$themeid,$is_end);
        }

        $page_total = $count;
        $list = giftModel::getGainRecord($themeid,$where_arr,$page_start,$page_size);
        foreach ($list as $key => $val){
            $gift_id = $val['gift_id'];
            if($gift_list[$gift_id]){
                 $gift = $gift_list[$gift_id];
                 $gift_name = $gift['name'];
                 $gift_img = $gift['top_img'];
                 $gift_price = $gift['price'];
                 $gift_type =  $gift['type'];
            }else{
                $gift_name = '';
                $gift_img = '';
                $gift_price = '';
                $gift_type = 1;
            }
            $list[$key]['gift_name'] = $gift_name;
            $list[$key]['gift_img'] = $gift_img;
            $list[$key]['gift_price'] = $gift_price;
            $list[$key]['gift_type'] = $gift_type;

            $taskid = $val['taskid'];
            if($val['classify']=='tuan_gift'){
                $list[$key]['classify_info'] = $tuan_config_data[$taskid].'人成团';
            }else{
                $list[$key]['classify_info'] = '邀请'.$invite_config_data[$taskid].'人';
            }

            if($val['workerid']){
                $r = Worker::getWokerName($_S,$themeid,$val['workerid']);
                $list[$key]['workername']=$r['workername'];
                $list[$key]['teamname']=$r['teamname'];
                $list[$key]['companyname']=$r['companyname'];
            }
        }
        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/gift/record/gain', $return_clicent);
    }
    //添加记录
    function addUserGiftRecord(){
        global $_G,$_P,$_S,$_C;
        $themeid = $_P['themeid'];
        $uid = $_P['uid'];
        $classify = $_P['classify'];
        $taskid = $_P['taskid'];
        $gift_id = $_P['gift_id'];
        $check = (int)db_fetchcolumn ( "SELECT COUNT(*) FROM " . tablename (t_intive_gift_record) ." WHERE uid = {$uid} AND taskid={$taskid} AND classify='{$classify}' AND themeid = {$themeid}");
        if($check>0){
             return_json(1,'已获得过,不可添加');
        }
        $res = giftModel::addUserGiftRecord($themeid,$uid,$classify,$taskid,$gift_id);
        if($res){
            return_json(0,'添加成功');
        }else{
            return_json(1,'添加失败');
        }
    }

    function validation()
    {
        global $_G,$_P;
        $themeid = $_P['themeid'];
        $page = $_P['page']-1;
        $res = giftModel::validation($themeid,$page);
        return_json(200,$res);
    }
    function gift_record_delete()
    {
        global $_G,$_P;
        $themeid = $_P['themeid'];
        //删除所有未核销的礼品
        db_delete(t_intive_gift_record, array('themeid'=>$themeid,'status'=>1));
    }

}